package task2;

import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 东莞呵呵
 * Date:2022-09-06
 * Time:20:46
 */
public class TestDemo2 {
    public static void main(String[] args) {
        int[] arr={3,3,3,3,2,2,2};
        System.out.println(MoreThanHalfNum_Solution(arr));
    }
    public static int MoreThanHalfNum_Solution(int [] array) {
        HashMap<Integer,Integer> map=new HashMap<>();
        for (int i = 0; i < array.length; i++) {
            if(map.containsKey(array[i])){
                int value=map.get(array[i]);
                map.put(array[i],value+1);
            }else {
                map.put(array[i],1);
            }
        }
        int max=0;
        int ret=0;
        for (int i = 0; i < array.length; i++) {
            if(map.get(array[i])>max){
                ret=array[i];
                max=map.get(array[i]);
            }
        }
        return ret;
    }
}
